/****************
 *@description: 收款合计
 *@author: Xhd
 *@date: 2023-03-30 11:21:32
 *@version: V1.0.0
*************************************************************************/

import React, { Component } from "react";
import { Space, Table } from "antd";
import GoBack from '@/components/TheGoBack';
import { apiDayPriceRecordCollectionPage, apiDayPriceRecordInOrOutSum } from '@/request/financial';
import { apiGetStoreInfoList } from '@/request/store';
import ThePagination from '@/components/ThePagination';
import TheQueryForm from '@/components/TheQueryForm';
import store from "@/store/store";
import { getDateTime3 } from '@/utils/getDate';
import '../style/style.less';
import moment from 'moment';
import _ from 'lodash';

class Collection extends Component {
  constructor(){
    super();
    this.state = {
      columns: [
        {
          title:'日期',
          dataIndex:'day',
          key:'day',
          align:'center'
        },
        {
          title:'门店名称',
          dataIndex:'storeName',
          key:'storeName',
          align:'center'
        },
        {
          title:'收款方式',
          children: [
            {
              title: '支付宝',
              children:[
                {
                  title: '商城',
                  dataIndex: 'shopInAlipay',
                  key: 'shopInAlipay',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '门店',
                  dataIndex: 'cashierInAlipay',
                  key: 'cashierInAlipay',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '自提',
                  dataIndex: 'offlineInAlipay',
                  key: 'offlineInAlipay',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                }
              ],
              align:'center'
            },
            {
              title: '微信',
              children:[
                {
                  title: '商城',
                  dataIndex: 'shopInWechat',
                  key: 'shopInWechat',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '门店',
                  dataIndex: 'cashierInWechat',
                  key: 'cashierInWechat',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '自提',
                  dataIndex: 'offlineInWechat',
                  key: 'offlineInWechat',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                }
              ],
              align:'center'
            },
            {
              title: '银行卡',
              children:[
                {
                  title: '商城',
                  dataIndex: 'shopInYspay',
                  key: 'shopInYspay',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '门店',
                  dataIndex: 'cashierInPos',
                  key: 'cashierInPos',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                },
                {
                  title: '自提',
                  dataIndex: 'offlineInYspay',
                  key: 'offlineInYspay',
                  align: 'center',
                  render: (text) => {
                    return <React.$Price price={text} />
                  }
                }
              ],
              align:'center'
            },
            {
              title: '现金',
              dataIndex: 'cashierInCash',
              key: 'cashierInCash',
              align:'center',
              render: (text) => {
                return <React.$Price price={text} />
              }
            },
          ],
          align:'center'
        },
        {
          title: "收款合计",
          dataIndex:'inAll',
          key:'inAll',
          align:'center',
          render: (text) => {
            return <React.$Price price={text} />
          }
        },
      ],
      form_show: true,
      searchOption:[
        { field: 'storeName', label: '门店名称', placeholder: '请选择', required: false, rules: [], type: 'Select', options: [], txt: 'storeName', value: 'storeName', api: apiGetStoreInfoList, defaultValue: '' },
        { field: 'Times', label: '日期', placeholder: '', required: false, rules: [], type: 'RangePicker', defaultValue: '', disabledDate:this.disabledDate },
      ],
      queryForm: {},
      dataSource:[],
      pageData:{
        pageNo:1,
        pageSize:10,
      },
      total:0,
      totalPriceSource:{}
    }
  }
  
  componentDidMount() {
    let searchParams = store.getState().searchValue
    let { pageNo, pageSize, queryForm, pathName } = searchParams
    if (searchParams && pageNo&&pathName==='moneyDaily') {
        this.setState({
            queryForm,
            pageData: {
                pageNo: pageNo, pageSize: pageSize
            }
        }, () => {
            this.queryFormRef.onSetDefault({
                ...queryForm
            })
            this.getData()
        })
    } else {
        this.getData()
    }
  }

  getData = async (values) => {
    let { pageNo, pageSize } = this.state.pageData
    if (values && !_.isEmpty(values.Times)) {
      values.createStartTime = getDateTime3(values.Times[0])
      values.createEndTime = getDateTime3(values.Times[1])
      delete values.Times
    }
    this.setState({
      queryForm: values === undefined ? this.state.queryForm : { ...values }
    }, async () => {
      const params = {
          pageNo, pageSize, queryForm: this.state.queryForm
      }
      store.dispatch({
          type: 'SaveSearchValues',
          data: {
            pathName:'moneyDaily',
              ...params
          }
      })
      let res = await apiDayPriceRecordCollectionPage({ pageNo, pageSize, ...this.state.queryForm })
      if(res.code === 0 && res.data){
        let request = await apiDayPriceRecordInOrOutSum({ ...this.state.queryForm })
        if(request.code === 0){
          this.setState({
            totalPriceSource:request.data
          })
        }
        this.setState({
          dataSource:res.data,
          pageData:{
            pageNo:res.pageNo,
            pageSize: res.pageSize,
          },
          total: res.totalCount
        })
      }
    })
    
  }
  disabledDate = (current) =>{
    return current && current > moment().startOf('day');
  }
  pageChange = (pageNo, pageSize) => {
    this.setState({
      pageData:{
        pageNo,
        pageSize,
      },
    },()=>{
      this.getData()
    })
  }
  
  onFormSearch(values){
    this.setState({
        pageData:{pageNo:1,pageSize:10}
    },()=>{
        this.getData(values)
    })
  }

  onReset() {
    this.setState({
        queryForm: {}
    }, () => {
        this.pageChange(1, 10)
    })
  }

  render(){
    const { columns, dataSource, pageData:{ pageNo, pageSize }, total, form_show, searchOption,totalPriceSource } = this.state
    return(
      <div className="financial-box">
        <GoBack history={this.props.history} title="收款合计" />
        <Space direction="vertical">
          <div></div>
          <TheQueryForm ref={(ref) => this.queryFormRef = ref} show={form_show} option={searchOption}
            onReset={() => this.onReset()}
            onGetValues={(values) => this.onFormSearch(values)} >
          </TheQueryForm>
        </Space>
        <Table bordered columns={columns} dataSource={dataSource} pagination={false} 
          summary={() => (
            <Table.Summary fixed>
                <Table.Summary.Row style={{textAlign: "center"}}>
                    <Table.Summary.Cell index={0}>合计:</Table.Summary.Cell>
                    <Table.Summary.Cell index={1} ></Table.Summary.Cell>
                    <Table.Summary.Cell index={2}>{ totalPriceSource.shopInAlipaySum ? '￥'+(totalPriceSource.shopInAlipaySum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={3}>{ totalPriceSource.cashierInAlipaySum ? '￥'+(totalPriceSource.cashierInAlipaySum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={4}>{ totalPriceSource.offlineInAlipaySum ? '￥'+(totalPriceSource.offlineInAlipaySum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={5}>{ totalPriceSource.shopInWechatSum ? '￥'+(totalPriceSource.shopInWechatSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={6}>{ totalPriceSource.cashierInWechatSum ? '￥'+(totalPriceSource.cashierInWechatSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={7}>{ totalPriceSource.offlineInWechatSum ? '￥'+(totalPriceSource.offlineInWechatSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={8}>{ totalPriceSource.shopInYspaySum ? '￥'+(totalPriceSource.shopInYspaySum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={9}>{ totalPriceSource.cashierInPosSum ? '￥'+(totalPriceSource.cashierInPosSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={10}>{ totalPriceSource.offlineInYspaySum ? '￥'+(totalPriceSource.offlineInYspaySum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={11}>{ totalPriceSource.cashierInCashSum ? '￥'+(totalPriceSource.cashierInCashSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                    <Table.Summary.Cell index={12}>{ totalPriceSource.inAllSum ? '￥'+(totalPriceSource.inAllSum / 100).toFixed(2) : '￥0.00' }</Table.Summary.Cell>
                </Table.Summary.Row>
            </Table.Summary>
          )}
        /> 
        <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
      </div>
    )
  }
}

export default Collection
